<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>数据</el-breadcrumb-item>
        <el-breadcrumb-item>书籍</el-breadcrumb-item>
        <el-breadcrumb-item>书籍数据搜索</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
      >
        <!-- 自定义按钮 -->
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click.stop="openDialog(1)"
            >添加</el-button
          >
        </template>
        <template slot="book_title" slot-scope="scope">
          <el-row>
            {{ scope.row.book_title + "(" + scope.row.book_auth + ")" }}
          </el-row>
        </template>
      </avue-crud>
    </basic-container>
    <!-- 添加弹出框start -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="40%"
      :modal="false"
    >
      <el-form ref="form" :model="form2" label-width="100px">
        <el-form-item label="APP名称:"> 橘子猫(轻小说) </el-form-item>
        <el-form-item label="图书ID:">
          <el-input type="number" v-model="form2.book_id"></el-input>
        </el-form-item>
        <el-form-item label="选择日期:">
          <el-date-picker v-model="form2.select_date" type="date">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { appBookDataSearch as baseUrl, bookCategory as book } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      dialogVisible: false,
      title: "添加",
      form: {},
      form2: {},
      page: {
        total: 0,
        pageSize: 10,
        // pageSizes:[1,2,3]
      },
      type: true,
      data: [],
      params: {},
      option: {
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: true,
        selection: true,
        border: true,
        menu: false,
        index: true,
        indexLabel: "序号",
        column: [
          {
            label: "选择日期",
            prop: "select_date",
            type: "input",
            width: 40,
          },
          {
            label: "一级分类",
            prop: "category_1",
            type: "select",
            dicData: [],
            props: {
              label: "name",
              value: "category_id",
            },
            width: 40,
          },
          {
            label: "二级分类",
            prop: "category_2",
            width: 40,
            type: "select",
            dicData: [],
            props: {
              label: "name",
              value: "category_id",
            },
          },
          {
            label: "图书ID",
            prop: "book_id",
            type: "input",
            search: true,
            width: 40,
          },
          {
            label: "图书名称(书名+作者)",
            prop: "book_title",
            type: "input",
            width: 90,
            slot: true,
          },
          {
            label: "状态",
            prop: "status",
            type: "select",
            dicData: [
              {
                label: "正在计算中",
                value: 0,
              },
              {
                label: "已完成",
                value: 1,
              },
            ],
            width: 40,
          },
          {
            label: "阅读UV",
            children: [
              {
                label: "前1天阅读UV",
                prop: "pre_day01_read_uv",
                type: "input",
                width: 40,
              },
              {
                label: "前2天阅读UV",
                prop: "pre_day02_read_uv",
                type: "input",
                width: 40,
              },
              {
                label: "前3天阅读UV",
                prop: "pre_day03_read_uv",
                type: "input",
                width: 40,
              },
              {
                label: "当天阅读UV",
                prop: "today_read_uv",
                type: "input",
                width: 40,
              },
              {
                label: "后1天阅读UV",
                prop: "next_day01_read_uv",
                type: "input",
                width: 40,
              },
              {
                label: "后2天阅读UV",
                prop: "next_day02_read_uv",
                type: "input",
                width: 40,
              },
              {
                label: "后3天阅读UV",
                prop: "next_day03_read_uv",
                type: "input",
                width: 40,
              },
            ],
          },
          {
            label: "阅读章节数",
            children: [
              {
                label: "前1天阅读章节数",
                prop: "pre_day01_read_chapter",
                type: "input",
                width: 40,
              },
              {
                label: "前2天阅读章节数",
                prop: "pre_day02_read_chapter",
                type: "input",
                width: 40,
              },
              {
                label: "前3天阅读章节数",
                prop: "pre_day03_read_chapter",
                type: "input",
                width: 40,
              },
              {
                label: "当天阅读章节数",
                prop: "today_read_chapter",
                type: "input",
                width: 40,
              },
              {
                label: "后1天阅读章节数",
                prop: "next_day01_read_chapter",
                type: "input",
                width: 40,
              },
              {
                label: "后2天阅读章节数",
                prop: "next_day02_read_chapter",
                type: "input",
                width: 40,
              },
              {
                label: "后3天阅读章节数",
                prop: "next_day03_read_chapter",
                type: "input",
                width: 40,
              },
            ],
          },
          {
            label: "人均阅读章节数",
            children: [
              {
                label: "前1天人均阅读章节数",
                prop: "pre_day01_avg_read_chapter",
                type: "input",
                width: 40,
              },
              {
                label: "前2天人均阅读章节数",
                prop: "pre_day02_avg_read_chapter",
                type: "input",
                width: 40,
              },
              {
                label: "前3天人均阅读章节数",
                prop: "pre_day03_avg_read_chapter",
                type: "input",
                width: 40,
              },
              {
                label: "当天人均阅读章节数",
                prop: "today_avg_read_chapter",
                type: "input",
                width: 40,
              },
              {
                label: "后1天人均阅读章节数",
                prop: "next_day01_avg_read_chapter",
                type: "input",
                width: 40,
              },
              {
                label: "后2天人均阅读章节数",
                prop: "next_day02_avg_read_chapter",
                type: "input",
                width: 40,
              },
              {
                label: "后3天人均阅读章节数",
                prop: "next_day03_avg_read_chapter",
                type: "input",
                width: 40,
              },
            ],
          },
          {
            label: "创建时间",
            prop: "create_time",
            type: "input"
          },
          {
            label: "选择日期",
            type: "daterange",
            prop: "dateselect",
            hide: true,
            search: true,
          },
        ],
      },
    };
  },
  methods: {
    openDialog(type) {
      this.title = "添加";
      this.dialogVisible = true;
    },
    async handleAdd() {
      let data = await common(
        `${baseUrl.addSearch}`,
        { ...this.form2 },
        undefined,
        "POST"
      );
      if (data.code != "1") {
        this.$message.error(data.message);
      } else {
        this.dialogVisible = false;
        this.getList();
      }
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      this.params.orderByList = "create_time&DESC";
      //发送请求获取数据
      let response = await common(
        `${baseUrl.pageAndParam}`,
        { ...this.params, ...this.defalut },
        this.page,
        "POST"
      );

      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      if (params.dateselect)
        params.select_date = `${
          params.dateselect ? "!<>!" + params.dateselect : ""
        }`;

      if (params.book_title)
        params.book_title = `${
          params.book_title ? "%" + params.book_title + "%" : ""
        }`;

      if (params.book_id)
        params.book_id = `${params.book_id ? "%" + params.book_id + "%" : ""}`;

      delete params.dateselect;
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
  async created() {
    let data = await common(
      book.getListByLeave + `/1`,
      null,
      null,
      "GET"
    );
    if (data.code == 1) {
      this.option.column[1].dicData = data.data;
    }
    let data2 = await common(
      book.getListByLeave + `/2`,
      null,
      null,
      "GET"
    );
    if (data2.code == 1) {
      this.option.column[2].dicData = data2.data;
    }
  },
};
</script>